package com.banshou.installs.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.banshou.installs.R;
import com.banshou.installs.constant.BaseApplication;
import com.banshou.installs.constant.Config;
import com.banshou.installs.model.dao.RequestLogin;
import com.banshou.installs.model.dao.ResponseLogin;
import com.banshou.installs.model.net.Observer;
import com.banshou.installs.model.net.ResponseInfo;
import com.banshou.installs.model.net.RetrofitManager;
import com.banshou.installs.model.net.RetrofitManager2;
import com.banshou.installs.model.net.RxUtil;
import com.banshou.installs.ui.view.ClearEditText;
import com.banshou.installs.utils.DialogUtils;
import com.banshou.installs.utils.SharePreUtil;
import com.banshou.installs.utils.StringUtil;
import com.banshou.installs.utils.ToastUtil;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cjq on 2017/12/28.
 * Email: stoic_yb@139.com
 * features:
 */

public class LoginActivity extends BaseActivity {
    @BindView(R.id.login_tel)
    ClearEditText loginTel;
    @BindView(R.id.login_pass)
    ClearEditText loginPass;
    @BindView(R.id.login_forget)
    TextView loginForget;
    @BindView(R.id.next)
    Button next;
    private DialogUtils dialogUtils;

    @SuppressLint("CheckResult")
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ButterKnife.bind(this);
       initView();
    }

    private void initView() {
        dialogUtils = new DialogUtils();
        ActivityUtils.finishAllActivitiesExceptNewest();
        loginPass.addTextChangedListener(passWacher);
        setButton();
    }

    private void setButton() {
        loginTel.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int count) {
                int length = s.toString().length();
                if (length == 13 && !TextUtils.isEmpty(loginPass.getText().toString())) {
                    next.setEnabled(true);
                } else {
                    next.setEnabled(false);
                }
                //删除数字
                if (count == 0) {
                    if (length == 4) {
                        loginTel.setText(s.subSequence(0, 3));
                    }
                    if (length == 9) {
                        loginTel.setText(s.subSequence(0, 8));
                    }
                }
                //添加数字
                if (count == 1) {
                    if (length == 4) {
                        String part1 = s.subSequence(0, 3).toString();
                        String part2 = s.subSequence(3, length).toString();
                        loginTel.setText(part1.concat(" ").concat(part2));
                    }
                    if (length == 9) {
                        String part1 = s.subSequence(0, 8).toString();
                        String part2 = s.subSequence(8, length).toString();
                        loginTel.setText(part1.concat(" ").concat(part2));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
                loginTel.setSelection(loginTel.getText().toString().length());
            }
        });
    }

    public TextWatcher passWacher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!TextUtils.isEmpty(loginPass.getText().toString()) && !TextUtils.isEmpty(loginTel.getText().toString())) {
                next.setEnabled(true);
                next.setClickable(true);
            } else {
                next.setEnabled(false);
                next.setClickable(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };


    @OnClick({R.id.login_forget, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            忘记密码
            case R.id.login_forget:
                Intent forget = new Intent(this, ForgetPassActivity.class);
                forget.putExtra(Config.FROMTOPASS, Config.LOGIN_FORGET_PASS);
                startActivity(forget);
                break;
//            进入主界面
            case R.id.next:
                String tel = loginTel.getText().toString().trim().replace(" ","");
                String pass = loginPass.getText().toString().trim();
                if (!RegexUtils.isMobileSimple(tel)) {
                    ToastUtil.toast("请输入正确的11位手机号");
                    return;
                }
//                String encode = StringUtil.encode(pass);
                RequestLogin requestLogin = new RequestLogin(tel, pass);
                dialogUtils.showAni(LoginActivity.this);
                RetrofitManager2.getInstance()
                        .create(ResponseInfo.class)
                        .login(requestLogin)
                        .compose(RxUtil.lifecycle(this))
                        .subscribe(new Observer<ResponseLogin>() {
                            @Override
                            protected void onNextStep(ResponseLogin login) {
                                dialogUtils.hideAni();
                                ToastUtil.toast("登录成功");
                                SharePreUtil.setTel(BaseApplication.getContext(), loginTel.getText().toString().trim());
                                SharePreUtil.setPower(BaseApplication.getContext(), login.getPower());
                                SharePreUtil.setName(BaseApplication.getContext(), login.getName());
                                SharePreUtil.setCompanyId(BaseApplication.getContext(), login.getCompany_id());
                                SharePreUtil.setCompanyName(BaseApplication.getContext(), login.getCompany_name());
                                SharePreUtil.saveToken(BaseApplication.getContext(), login.getToken());
                                if (login.getPower() == 4 && login.getFirst()) {
                                    Intent intent = new Intent(LoginActivity.this, ForgetPassActivity.class);
                                    intent.putExtra(Config.FROMTOPASS, Config.LOGIN_FIRST_PASS);
                                    startActivity(intent);
                                } else {
                                    Intent next = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(next);
                                    finish();
                                }
                            }

                            @Override
                            protected void onErrorResult() {
                                dialogUtils.hideAni();
                            }
                        });
                break;
        }
    }
}
